package com.hk.activity;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gc.materialdesign.views.ButtonFlat;
import com.hk.R;
import com.hk.biz.BLL;
import com.hk.model.Question;
import com.hk.util.Utils;
import com.umeng.analytics.MobclickAgent;

public class Question2Activity extends Activity {

	private TextView mtvTitle;
	private Button mbtnChangeColor;
	private ViewPager viewPager;
	private TextView mtvNowQuestion;
	private TextView mtvSumQuestion;
	private LinearLayout mrlAnswer;
	private TextView mtvAnswer;
	private TextView mtvAnalysis;
	private TextView mtvhead;
	private ButtonFlat mbtnShowAnswer;
	private Button mbtn_back;

	private String mQType,mHead;
	private boolean useTheme = true;
	private Bundle savedInstanceState;
	private List<View> viewList;
	private List<Question> listQuestion;
	private static int pos = 0;// 当前题目
	private static int all = 3;// 总题目数
	private String mPageName="Question2Activity";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (useTheme) {
			setTheme(R.style.ThemeDay);
		} else {
			setTheme(R.style.ThemeNight);
		}

		setContentView(R.layout.activity_question2);
		this.savedInstanceState = savedInstanceState;
		findByID();
		
		Bundle bundle = this.getIntent().getExtras();
		mQType = bundle.getString("QuestionType");
		mHead= bundle.getString("Head");
		getNowQuestion();

		listQuestion = BLL.getQuestionListByType(getApplicationContext(),
				mQType);
		all = listQuestion.size();

		viewList = new ArrayList<View>();
		LayoutInflater mLi = LayoutInflater.from(this);
		for (int i = 0; i < all; i++) {
			View v = mLi.inflate(R.layout.viewpage_question2, null);
			viewList.add(v);
		}

		setIndicate();

		viewPager.setAdapter(new MyAdapter());
		viewPager.setOnPageChangeListener(new MyPagerChangeListener());
		viewPager.setCurrentItem(pos);
		
		mbtnChangeColor.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				changeColor();
			}
		});
		
		mbtn_back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		mtvhead.setText(mHead);
		
		setBtnBg();
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart( mPageName );
		MobclickAgent.onResume(this);
	}
	
	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd( mPageName );
		MobclickAgent.onPause(this);
	}
	
	@Override
	public void finish() {
		super.finish();
	}
	
	public void setBtnBg(){
		if(useTheme){
			mbtnChangeColor.setBackgroundResource(R.drawable.btn_night);
		}else{
			mbtnChangeColor.setBackgroundResource(R.drawable.btn_sun);
		}
	}


	// 查询ID
	protected void findByID() {
		viewPager = (ViewPager) findViewById(R.id.vpQuestion);
		mbtnChangeColor = (Button) this.findViewById(R.id.btnChangeColor);
		mtvNowQuestion = (TextView) this.findViewById(R.id.tvNowQuestion);
		mtvSumQuestion = (TextView) this.findViewById(R.id.tvSumQuestion);
		mbtn_back=(Button)this.findViewById(R.id.btn_back);
	    mtvhead= (TextView)this.findViewById(R.id.tvhead);
	}

	// 修改主题
	public void changeColor() {
		useTheme = !useTheme;
		onCreate(Question2Activity.this.savedInstanceState);
	}

	// 设置当前题目数和总题目数
	public void setIndicate() {
		mtvNowQuestion.setText(String.valueOf(pos + 1));
		mtvSumQuestion.setText(String.valueOf(all));
	}

	public void setNowQuestion() {
		Utils.putValue(getApplicationContext(), "Question_" + mQType,
				String.valueOf(pos));
	}

	public void getNowQuestion() {
		String val = Utils.getValue(getApplicationContext(), "Question_"
				+ mQType);
		if (val != null && !val.equals("")) {
			pos = Integer.valueOf(val);
		} else {
			pos = 0;
		}
	}

	// ViewPage的数据项
	public class MyAdapter extends PagerAdapter {

		MyAdapter() {

		}

		@Override
		public int getCount() {
			return all;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {

			View v = viewList.get(position);
			Question q = listQuestion.get(position);
		
			mtvTitle = (TextView) v.findViewById(R.id.tvTitle);
			mtvAnswer = (TextView) v.findViewById(R.id.tvAnswer);
			mtvAnalysis = (TextView) v.findViewById(R.id.tvAnalysis);
			mbtnShowAnswer =(ButtonFlat)v.findViewById(R.id.btnShowAnswer);
			
			mtvAnswer.setText(q.Answer);
			mtvAnalysis.setText(q.Analysis);
			mtvTitle.setText(q.Title);
			
			mbtnShowAnswer.setOnClickListener(new ChooseClickedListener());
			
			container.addView(v);
			return v;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(viewList.get(position));
		}
	}

	// ViewPage的页面监听
	class MyPagerChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onPageSelected(int position) {
			pos = position;
			setIndicate();
			setNowQuestion();
		}
	}

	// 答案监听
	class ChooseClickedListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			View view = viewList.get(pos);
			mrlAnswer = (LinearLayout) view.findViewById(R.id.rlAnswer);
			
			if(mrlAnswer.getVisibility()==View.GONE){
				mrlAnswer.setVisibility(View.VISIBLE);
			}else{
				mrlAnswer.setVisibility(View.GONE);
			}
		}
	}

}
